package servlet;

import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		System.out.println("/register");
		String account = request.getParameter("userName");
		String password = request.getParameter("password");
		String phone = request.getParameter("mobile");
		String email = request.getParameter("email");
		String realName = request.getParameter("realName");
		String nickname = request.getParameter("nickname");
		UserDao dao = new UserDao();
		int retSureAccount = dao.sure_user(account);
		if(retSureAccount >= 1){
			//用户名已存在
			request.getSession().setAttribute("errorMsg", "用户名已存在");
			request.getRequestDispatcher("frontend/register.jsp").forward(request, response);
			return;
		}
		int ret = dao.register(account,password,phone,email,realName,nickname);
		if(ret >= 1){
			request.getSession().setAttribute("successMsg", "注册成功,请重新登录！");
			response.sendRedirect("frontend/index.jsp");
		}

	}
}
